package com.epfresh.delegate;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.bean.ProductCategory;
import com.epfresh.bean.StoreDetail;
import com.epfresh.global.BaseActivity;
import com.epfresh.views.dialog.BaseDialogView;
import com.epfresh.views.tag.FlowLayout;
import com.epfresh.views.tag.TagAdapter;
import com.epfresh.views.tag.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 17-2-21
 * @Description
 */

public class DcFilterDelegate implements View.OnClickListener {
    BaseActivity activity;
    StoreDetail storeDetail;

    TextView tvClear;
    TextView tvSure;
    TagFlowLayout filterTag;

    View vBg;
    View vParent;


    TagAdapter<ProductCategory> adapter;
    List<ProductCategory> names = new ArrayList<>();

    OnFilter onFilter;
    BaseDialogView.OnDismissListener onDismissListener;

    public DcFilterDelegate(BaseActivity activity, StoreDetail storeDetail) {
        this.activity = activity;
        this.storeDetail = storeDetail;
        initView();
    }
    public DcFilterDelegate(BaseActivity activity) {
        this.activity = activity;
        initView();
    }

    public BaseDialogView.OnDismissListener getOnDismissListener() {
        return onDismissListener;
    }

    public void setOnDismissListener(BaseDialogView.OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
    }

    public OnFilter getOnFilter() {
        return onFilter;
    }

    public void setOnFilter(OnFilter onFilter) {
        this.onFilter = onFilter;
    }

    public View findViewById(int id) {
        return activity.findViewById(id);
    }


    private void initView() {

        tvClear = (TextView) findViewById(R.id.tv_clear);
        tvSure = (TextView) findViewById(R.id.tv_sure);
        vBg = findViewById(R.id.v_bg);
        vParent = findViewById(R.id.rl_filter);
        filterTag = (TagFlowLayout) findViewById(R.id.ft_filter);

        tvClear.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        vBg.setOnClickListener(this);

        adapter = new TagAdapter<ProductCategory>(names) {

            @Override
            public View getView(FlowLayout parent, int position, ProductCategory comm) {
                TextView tv = (TextView) LayoutInflater.from(activity).inflate(R.layout.dc_tag,
                        parent, false);
                tv.setText(comm.getName());
                return tv;
            }
        };

        filterTag.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                updateSelected(selectPosSet);
            }
        });

        filterTag.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (position == 0) {
                    adapter.setSelectedList(0);
                    return true;
                }
                return false;
            }
        });

        filterTag.setAdapter(adapter);
    }

    private void updateSelected(Set<Integer> selectPosSet) {

        if (selectPosSet != null) {

            if (((selectPosSet.size() == 1 && selectPosSet.contains(0))) || selectPosSet.size() == 0) {
                adapter.setSelectedList(0);
            } else {
                selectPosSet.remove(0);
                adapter.setSelectedList(selectPosSet);

            }
        } else {
            adapter.setSelectedList(0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_clear:
                clear();
                break;
            case R.id.tv_sure:
                sure();
                break;
            case R.id.v_bg:
                dismiss();
                break;
        }
    }

    private void sure() {


        Set<Integer> set = filterTag.getSelectedList();
        List<String> name = null;
        for (Integer i : set) {
            ProductCategory p = names.get(i);
            String id = p.getId();
            if (id != null && !"".equals(id)) {
                if (name == null) {
                    name = new ArrayList<>();
                }
                name.add(id);
            }
        }

        if (onFilter != null) {
            onFilter.onFilter(set, name);
        }
        dismiss();
    }

    private void clear() {
        adapter.setSelectedList(0);
    }

    public boolean isShow() {
        return vParent.getVisibility() == View.VISIBLE;
    }

    public void show() {
        vParent.setVisibility(View.VISIBLE);
    }

    public void dismiss() {
        vParent.setVisibility(View.GONE);
        if (onDismissListener != null ){
            onDismissListener.onDismiss(null);
        }
    }

    public void show(List<ProductCategory> names, Set<Integer> set) {

        this.names.clear();
        ProductCategory p = new ProductCategory();
        p.setName("全部");
        this.names.add(p);
        if (names != null && names.size() > 0)
            this.names.addAll(names);

//        p = new ProductCategory();
//        p.setName("水果1");
//        p.setId("1");
//        this.names.add(p);
//        p = new ProductCategory();
//        p.setName("水果133");
//        this.names.add(p);
//        p = new ProductCategory();
//        p.setName("水果155");
//        this.names.add(p);
//        p = new ProductCategory();
//        p.setName("水果23");
//        this.names.add(p);
//        p = new ProductCategory();
//        p.setName("水果532");
//        this.names.add(p);
//        p = new ProductCategory();
//        p.setName("水果432");
//        this.names.add(p);
//        p = new ProductCategory();
//        p.setName("水果666");
//        this.names.add(p);
        if (this.names.size() == 1 || set == null || set.size() == 0) {
            adapter.setSelectedList(0);
//            adapter.notifyDataChanged();
        } else {
            adapter.setSelectedList(set);
//            adapter.notifyDataChanged();
        }

        show();
    }

    interface OnFilter {
        void onFilter(Set<Integer> set, List<String> names);
    }

}
